package com.xiaoma.verbal.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.xiaoma.tpo.baseDao.DefaultRowMapper;
import com.xiaoma.tpo.utils.Pager;
import com.xiaoma.verbal.baseDao.BaseDaoImpl;
import com.xiaoma.verbal.domain.FctForecast;
import com.xiaoma.verbal.domain.FctQuestion;
import com.xiaoma.verbal.dto.FctForecastVO;

@Repository
public class FctForecastDao extends BaseDaoImpl<FctForecast> {
	private static final Logger LOG = LoggerFactory.getLogger(FctForecastDao.class);

	@SuppressWarnings("unchecked")
	public List<FctForecast> findTop3() {
		String sql = "SELECT * FROM fct_forecast f WHERE f.status = 1 and DATE_FORMAT(f.time_range1,'%Y-%m-%d')>=CURDATE()  order  by f.time_range1 asc limit 3";
		LOG.info("SQL:{} PARAMS:{}", sql);
		List<?> result = jdbcTemplate.query(sql, new DefaultRowMapper(FctForecast.class, getNameHandler()));
		return (List<FctForecast>) result;
	}
	//查询所有question 主要是ID
	@SuppressWarnings("unchecked")
	public List<FctQuestion> findAllForcastNew(Integer id) {
		String sql = "SELECT * "
				+ " FROM fct_forecast f ,fct_question fq"
				+ " WHERE f.status = 1 "
				+ " and DATE_FORMAT(f.time_range1,'%Y-%m-%d')>=CURDATE()  "
				+ " and f.id = fq.forecast_id and f.cls_class_id= ?"
				+ " order  by fq.create_time asc ";
		LOG.info("SQL:{} PARAMS:{}", sql);
		List<?> result = jdbcTemplate.query(sql, new DefaultRowMapper(FctQuestion.class, getNameHandler()),id);
		return (List<FctQuestion>) result;
	}
	public FctForecast findForecastByfnum(String fnum) {
		// TODO Auto-generated method stub
		String sql = " SELECT * FROM fct_forecast f WHERE f.forecast_num = ?";
		LOG.info("SQL:{} PARAMS:{}", sql);
		List<?> result = jdbcTemplate.query(sql, new DefaultRowMapper(FctForecast.class, getNameHandler()), fnum);
		if (!CollectionUtils.isEmpty(result)) {
			return (FctForecast) result.get(0);
		}
		return null;
	}

	public Pager queryPageList(Integer fid, Integer forecastNum, String timeRange, Integer status, Integer top, Integer startIndex, Integer itemsPerPage) {
		// TODO Auto-generated method stub

		Pager pager = new Pager();
		List<Object> params = new ArrayList<Object>();
		String sqlQuery = " ";
		String sqlCount = " ";
		sqlQuery = "select f.id, f.time_range1, "
				+ " f.time_range2, f.create_time, "
				+ " f.update_time, f.create_id, "
				+ " f.status, f.forecast_num, "
				+ " f.cls_class_id, "
				+ " cc.name, "
				+ " cc.desc, cc.recommended,"
				+ " cc.frequency, cc.img_url,"
				+ " cc.seq_num, cc.type ";
		String sqlTrunk = "  FROM fct_forecast f ,cls_class cc";
		String where = " where 1=1 and cc.id=f.cls_class_id";
		String orderBy = " order by f.time_range1 asc ";
		String limit = " limit ?,? ";
		if (fid != null && !"".equals(fid)) {
			where = where + " and f.id = ? ";
			params.add(fid);
		}
		if (forecastNum != null && !"".equals(forecastNum)) {
			where = where + " and f.forecast_num = ? ";
			params.add(forecastNum);
		}
		if (top != null) {
			where = where + " and DATE_FORMAT(f.time_range1,'%Y-%m-%d')>=CURDATE() ";
			where = where + " and f.status = ? ";
			params.add(1);
		}
		if (timeRange != null && !"".equals(timeRange)) {
			where = where + " and DATE_FORMAT(f.time_range1,'%Y-%m-%d')>=? ";
			params.add(timeRange);

		}
		if (status != null && !"".equals(status)) {
			where = where + " and f.status = ? ";
			params.add(status);
		}

		sqlQuery = sqlQuery + sqlTrunk + where + orderBy + limit;
		sqlCount = "select count(*)   " + sqlTrunk + where;
		LOG.info(sqlQuery + " PARAMS: " + startIndex + ", " + itemsPerPage);
		LOG.info(sqlCount);
		int totalCount = jdbcTemplate.queryForInt(sqlCount, params.toArray());

		
		if (top != null) {
			params.add(0);
			params.add(top);
		}else{
			params.add(startIndex);
			params.add(itemsPerPage);
		}
		List<?> list = jdbcTemplate.query(sqlQuery, params.toArray(), new DefaultRowMapper(FctForecastVO.class, this.getNameHandler()));

		pager.setList(list);
		pager.setTotalCount(totalCount);
		return pager;

	}

}
